package cn.edu.imnu.vacate.users.back.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import cn.edu.imnu.common.controller.BaseController;
import cn.edu.imnu.common.util.ConstatFinalUtil;
import cn.edu.imnu.vacate.users.enums.AUserEnum;
import cn.edu.imnu.vacate.users.pojo.AUser;
import cn.edu.imnu.vacate.users.service.IUsersService;

/**
 * 不需要登录的操作
 * @author MCR
 *
 */
@Controller
public class NoLoginBackController extends BaseController
{
	@Resource
	private IUsersService usersService;
	/**
	 * 管理员列表(测试)
	 * @return
	 */
	@RequestMapping("/userList")
	public String userList(HttpServletRequest request)
	{
		ConstatFinalUtil.LOGGER.info("------userList------");
		Map<String, Object> condMap = new HashMap<String, Object>();
		List<AUser> userList = this.usersService.findCondListUserService(null, condMap );
		request.setAttribute("userList", userList);
		return "/back/userList";
	}
	
	/**
	 * 登陆
	 * 打开登陆页面
	 * @return
	 */
	@RequestMapping("/login")
	public String login()
	{
		ConstatFinalUtil.LOGGER.info("------login------");
		
		return "/back/login";
	}
	
	/**
	 * 登陆提交
	 * @return
	 */
	@RequestMapping("/loginSubmit")
	public String loginSubmit(HttpServletRequest request,HttpServletResponse response)
	{
		ConstatFinalUtil.LOGGER.info("------loginSubmit------");
		HttpSession session = request.getSession();
		/*接收参数*/
		String loginName = request.getParameter("loginName");
		String password = request.getParameter("password");
		String code = request.getParameter("code");
		/*从session中取出验证码图片的验证*/
		String codeSess = session.getAttribute("rand") + "";
		/**
		 * 对比验证码
		 * 万能验证码:0815
		 */
		if((codeSess !=null && codeSess.equalsIgnoreCase(code)) || "0815".equalsIgnoreCase(code))
		{
			/*根据学号查询信息*/
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("loginName", loginName);
			AUser user = this.usersService.findOneUserService(condMap );
			if(user != null )
			{
				/*学号正确,验证密码*/
				if(user.getPassword().equalsIgnoreCase(password))
				{
					/*密码正确，验证状态*/
					if(user.getStatus() == AUserEnum.STATUS_ENABLE.getStatus())
					{
						/*启用
						 * 放入session*/
						session.setAttribute("user", user);
						session.setAttribute("lastLoginTime", user.getLastLoginTime());
						
						/*修改用户信息*/
						 user.setLastLoginTime(new Date());
						 this.usersService.updateOneUserService(user);
						/*启用状态,登陆成功*/
						return "redirect:/back/user/main.htm";
					}else
					{
						/*
						 * 用户被禁用 请联系管理员
						 */
						this.info = ConstatFinalUtil.INFO_JSON.getString("4");
					}
				}else
				{
					/*密码不正确*/
					this.info = ConstatFinalUtil.INFO_JSON.getString("5");
				}
				request.setAttribute("loginName", loginName);
			}else
			{
				/*学号不正确*/
				this.info = ConstatFinalUtil.INFO_JSON.getString("6");
			}
		}else
		{
			/*验证码不正确*/
			this.info = ConstatFinalUtil.INFO_JSON.getString("7");
		}
		request.setAttribute("info", info);
		System.out.println("------" + info);
		return this.login();
	}
	
	/**
	 * 打开注册页面
	 * @return
	 */
	@RequestMapping("/regsiter")
	public String regsiter()
	{
		ConstatFinalUtil.LOGGER.info("-----regsiter注册----");
		return "/back/regsiter";
	}
	
	/**
	 * 注册提交
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/regsiterSubmit",produces="text/html;charset=UTF-8")
	public String regsiterSubmit(HttpServletRequest request,AUser user)
	{
		ConstatFinalUtil.LOGGER.info("------regsiter注册提交--------");
		/*驱动封装*/
		user.setStatus(AUserEnum.STATUS_ENABLE.getStatus());
		user.setCreateTime(new Date());
		user.setUpdateTime(new Date());
		user.setLastLoginTime(new Date());
		/*保存数据库*/
		JSONObject resultJSON = this.usersService.saveOneUserService(user);
		/*提示用户*/
		this.info = resultJSON.getString("info");
		request.setAttribute("info", info);
		return this.regsiter();
	}
}
